#include "event_app.h"
#include "battery_app.h"
#include "protocol_app.h"
#include "bluetooth_board.h"
#include "device_parameter.h"
#include "central.h"

void BluetoothGetMacAddress(uint8_t *pMacOut)
{
  uint8_t MacAddr[MAC_ADDR_CODING_LENGTH] = {0};

  if (pMacOut != NULL)
  {
    GetMACAddress(MacAddr);
    for (uint8_t i = 0; i < MAC_ADDR_CODING_LENGTH; i++)
    {
      pMacOut[i] = MacAddr[(MAC_ADDR_CODING_LENGTH - 1) - i];
    }
  }
}

uint32_t BluetoothGetUpdateRadioInterval(void)
{
  return (GetBluetoothUpdateRaidioInterval() * 60) * 1000;
}

void BluetoothPostConnectionActions(void)
{
  EventSetBluetoothConnectFlagBit(true);
  EventStartStadnbyTask();
}

void BluetoothPostDisconnectionActions(void)
{
  EventExitStadnbyTask();
  EventSetBluetoothConnectFlagBit(false);
}

uint8_t BluetoothGetDeviceType(void)
{
  return GetObtainTheDeviceModel();
}

uint8_t BluetoothGetSystemStatus(void)
{
  return GetDeviceInitStatus();
}

void BluetoothGetRadioName(void *pNameOut)
{
  if (pNameOut != NULL)
  {
    char RadioName[30] = {0};
    uint32_t RoomNumberId = 0;
    uint8_t RadioNameLen = 0, DeviceInitStatus = 0;
    uint8_t GardenId = 0, BuildingId = 0, FloorId = 0, SubroomNumberId = 0;

    FloorId = GetLockFloorNumber();
    GardenId = GetLockGardenIdNumber();
    RoomNumberId = GetLockRoomIdNumber();
    BuildingId = GetLockBuildingNumber();
    SubroomNumberId = GetLockSubroomNumber();

    DeviceInitStatus = GetDeviceInitStatus() & (~FIRMWARE_UPGRADE);
    if (DeviceInitStatus == NORMAL_MODE)
    { 
      // 修改梯控的广播字段的显示
      if (BuildingId > 0)
      {
        sprintf(RadioName, "B%03d", BuildingId);
      }else
      {
        RadioNameLen = 8;
        strcpy(RadioName, "ERR_NAME");
      }

      RadioNameLen = strlen(RadioName);
      memcpy(pNameOut, RadioName, RadioNameLen);
    }
    else
    {
      PRINT("0000000000000000000000000 DEFAULT_BLUETOOTN_RADIO_NAME = %s\r\n", DEFAULT_BLUETOOTN_RADIO_NAME);
      memcpy(pNameOut, DEFAULT_BLUETOOTN_RADIO_NAME, strlen(DEFAULT_BLUETOOTN_RADIO_NAME));
    }
  }
}

uint8_t BluetoothGetProtocolVersion(void)
{
  return PROTOCOL_VERSION_CONTORL;
}

uint8_t BluetoothGetEventToBeSendFlagBit(void)
{
  if (EventGetLogNeedToBeUploadedFlagBit() == true)
    return LOG_AVAILABLE;

  return NO_LOG_AVAILABLE;
}

uint8_t BluetoothGetBatteyLevel(void)
{
  return BatteryGetVoltagePercentage();
}

uint16_t BluetoothGetConnectTimeout(void)
{
  uint16_t ConnectTimeout = GetBluetoothConnectionTimeout();
  if ((ConnectTimeout < MIX_CONNECTION_TIMEOUT) || (ConnectTimeout > MAX_CONNECTION_TIMEOUT))
  {
    ConnectTimeout = DEFAULT_CONNECTION_TIMEOUT;
  }

  return ConnectTimeout * 1000;
}

uint16_t BluetoothGetBroadcastInterval(void)
{
  return BLUETOOTH_BROADCAST_INTERVAL;
}

void BluetoothAnalysisServerPushMessage(const uint8_t *pMessageIn, const uint16_t SizeOfMessage)
{
  ProtocolPraseMessage(SOURCE_MOBILE_CLIENT, pMessageIn, SizeOfMessage);
}


bool BluetoothGetDeviceActivationStatus(void)
{
  uint8_t WorkMode = GetDeviceInitStatus() & (~FIRMWARE_UPGRADE);
  if (WorkMode == NORMAL_MODE)
    return true;

  return false;
}